<?php
/*
 *   This file is part of poat.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 */
 
include_once(ROOT_PATH."includes/common.php");

require_once(ROOT_PATH."class/files.class.php");
require_once(ROOT_PATH."class/status.class.php");
require_once(ROOT_PATH."class/user.class.php");
require_once(ROOT_PATH."class/history.class.php");
require_once(ROOT_PATH."class/project.class.php");
require_once(ROOT_PATH."class/team.class.php");

$notify=false;

$CURRENT_USER = new user;
$CURRENT_USER->userid=$_SESSION['s_user'];
$loader->get($CURRENT_USER);

debug("update");

$files = new files($_SESSION['projectid']);

$files->id=$data['id'];
$loader->get($files);

if ($files->stamp == $data['fstamp']) {
	$files->statusid=$data['newstatus'];
	$files->comment=$data['comment'];
	
	if ($CURRENT_USER->groupid == GROUP_COORD and $CURRENT_USER->userid != $data['assignto']) {
		$files->userid=$data['assignto'];
		$notify=true;
	} else {
		$files->userid=$CURRENT_USER->userid;
	}	
	
	$files->stamp=util::hashcode($files->id);
	$loader->update($files);
	
	$his = new history($_SESSION['projectid']);
	$his->statusid=$files->statusid;
	$his->fileid=$files->id;
	$his->userid=$CURRENT_USER->userid;
	$his->comment=$files->comment;
	$loader->save($his);
	
	sendFileChangeNotification($files);
	if ($notify) {
		if ($files->statusid == FILE_STATUS_LOCKED) {
			notifyUser($data['assignto'],$files);
		}
	}

} else {
	$ar = array($files->userid);
	$message=createMessage($LANG['updateconflict'],$ar);
}

$prj = new project();
$prj->id=$_SESSION['projectid'];
$loader->get($prj);

$team = new team;
if (isset($data['teamid'])) {
	$team->id = $data['teamid'];
} else {
	$team->id = $CURRENT_USER->teamid;
}
$loader->get($team);

$sprj = new project();
$sprj->id=$files->subprj;
$loader->get($sprj);

$files->lastupdate = util::fromUTC($files->lastupdate,$CURRENT_USER->timezone);
$files->url = VIEW_SVN_URL . $prj->path . str_replace('$LANG',$team->lang,$sprj->path) . "/" . $files->name;

$tm->assign("row",$files);
$tm->assign("CURRENT_USER",$CURRENT_USER);
$tm->assign("lang",$LANG);
$content = $tm->fetch('filerow.tpl');

?>